package com.john.bms.controller.system;

import com.github.pagehelper.PageInfo;
import com.john.bms.model.result.RequestResult;
import com.john.bms.model.system.role.RolePage;
import com.john.bms.model.system.role.RolePower;
import com.john.bms.model.system.role.SysRole;
import com.john.bms.service.system.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.john.bms.utils.CollectionUtils.map;

/**
 * @Author:张家小帅哥
 * @Date:2020/5/30
 * @Version:1.0
 * @Description:TODO
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @GetMapping("getRoles")
    public RequestResult getRoles() {
        List<Map<String, String>> roleList = roleService.getRoles();
        return RequestResult.of(map(roleList, "data"));
    }

    @GetMapping("getRoleNames")
    public RequestResult getRoleNames() {
        Map<String, Object> list = roleService.getRoleNames();
        return RequestResult.of(map(list, "data"));
    }

    @RequiresPermissions("role:view")
    @PostMapping("rolePageList")
    public RequestResult rolePageList(RolePage rolePage) {
        PageInfo<SysRole> roleList = roleService.getRoleList(rolePage);
        return RequestResult.of(map(roleList, "data"));
    }

    @PostMapping("updateRoleState")
    public RequestResult updateRoleState(int rid, int state) {
        boolean b = roleService.updateRoleState(rid, state);
        return RequestResult.of(map(b, "data"));
    }

    @GetMapping("deleteRole")
    public RequestResult deleteRole(int rId) {
        boolean b = roleService.deleteRole(rId);
        return RequestResult.of(map(b, "data"));
    }

    @PostMapping("roleAdd")
    public RequestResult roleAdd(SysRole sysRole) {
        boolean b = roleService.insertRole(sysRole);
        return RequestResult.of(map(b, "data"));
    }

    @GetMapping("roleInfo")
    public RequestResult roleInfo(int rId) {
        SysRole roleInfo = roleService.getRoleInfo(rId);
        return RequestResult.of(map(roleInfo, "data"));
    }

    @PostMapping("updateRole")
    public RequestResult updateRole(SysRole sysRole) {
        boolean b = roleService.updateRole(sysRole);
        return RequestResult.of(map(b, "data"));
    }

    @PostMapping("authorize")
    public RequestResult authorize(RolePower rolePower) {
        boolean b = roleService.authorize(rolePower);
        return RequestResult.of(map(b, "data"));
    }

    @GetMapping("getPowerList")
    public RequestResult getPowerList(int rId) {
        List<Integer> powerList = roleService.getPowerList(rId);
        return RequestResult.of(map(powerList, "data"));
    }

}
